package com.syedu.day05;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;

/**
 * description：数组的元素排序
 * 1.JDK内置方法进行排序,Arrays.sort(...)方法
 * 2.自己通过算法实现,冒泡,选择,快速,插入...(案例参见ArrayUtil)
 *
 * @author Administrator
 * @date 2022/3/11 15:26
 */
public class ArrayDemo3 {
    public static void main(String[] args) {
        Integer[] intArr = {2, 7, 3, 3, 2, 4};
        String[] strArr = {"B", "c", "A", "D"};
        String[] numberArr = {"13", "2", "25", "8", "11"};

        //\u4f60\u6211\u4ed6\u5b83
        String[] wordArr = {"你", "我", "他", "它"};
        //JDK内置的数组排序方法
        //默认按照字典顺序(0(48)-9,A(65)-Z,a(97)-z)进行从小到大排序
        /*Arrays.sort(intArr);
        Arrays.sort(strArr);
        Arrays.sort(numberArr);
        Arrays.sort(wordArr);*/

        //从大到小排序
        //注意: 基本类型需要提升为包装类才可以使用,即int->Integer,char->Character,其他都是首字母大写
        Arrays.sort(intArr, Collections.reverseOrder());
        Arrays.sort(strArr, Collections.reverseOrder());
        Arrays.sort(numberArr, Collections.reverseOrder());
        Arrays.sort(wordArr, Collections.reverseOrder());

        //打印结果
        System.out.println(Arrays.toString(intArr));
        System.out.println(Arrays.toString(strArr));
        System.out.println(Arrays.toString(numberArr));
        System.out.println(Arrays.toString(wordArr));

        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");

        //补充内容
        //模拟5个学生
        Student s1 = new Student("CC", 25, 90);
        Student s2 = new Student("AA", 21, 85);
        Student s3 = new Student("DD", 18, 92);
        Student s4 = new Student("BB", 19, 65);
        Student s5 = new Student("EE", 28, 59);
        //创建数组存储这5个学生
        Student[] students = {s1, s2, s3, s4, s5};
        //按照指定的规则来进行数据排序
        Arrays.sort(students, Comparator.comparing(Student::getAge));
        System.out.println(Arrays.toString(students));

        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
    }
}

class Student {
    private String name;//姓名
    private Integer age;//年龄
    private Double score;//分数

    public Student() {
    }

    public Student(String name, int age, double score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Double getScore() {
        return score;
    }

    public void setScore(Double score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }
}